package com.sp.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.sp.common.exception.ServiceException;
import com.sp.common.utils.DateUtils;
import com.sp.system.domain.SysParkingSpot;
import com.sp.system.service.ISysParkingSpotService;
import com.sp.system.service.ISysVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sp.system.mapper.SysParkingRecordMapper;
import com.sp.system.domain.SysParkingRecord;
import com.sp.system.service.ISysParkingRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 停车记录Service业务层处理
 *
 *
 * @date 2024-11-24
 */
@Service
public class SysParkingRecordServiceImpl implements ISysParkingRecordService {
    @Autowired
    private SysParkingRecordMapper sysParkingRecordMapper;

    @Autowired
    private ISysParkingSpotService sysParkingSpotService;

    @Autowired
    private ISysVehicleService sysVehicleService;

    /**
     * 查询停车记录
     *
     * @param id 停车记录主键
     * @return 停车记录
     */
    @Override
    public SysParkingRecord selectSysParkingRecordById(Long id) {
        SysParkingRecord sysParkingRecord = sysParkingRecordMapper.selectSysParkingRecordById(id);
        sysParkingRecord.setSysVehicle(sysVehicleService.selectSysVehicleById(sysParkingRecord.getVehicleId()));
        sysParkingRecord.setSysParkingSpot(sysParkingSpotService.selectSysParkingSpotById(sysParkingRecord.getParkingSpotId()));
        return sysParkingRecord;
    }

    /**
     * 查询停车记录列表
     *
     * @param sysParkingRecord 停车记录
     * @return 停车记录
     */
    @Override
    public List<SysParkingRecord> selectSysParkingRecordList(SysParkingRecord sysParkingRecord) {
        return sysParkingRecordMapper.selectSysParkingRecordList(sysParkingRecord)
                .stream().peek(e -> {
                    e.setSysParkingSpot(sysParkingSpotService.selectSysParkingSpotById(e.getParkingSpotId()));
                    e.setSysVehicle(sysVehicleService.selectSysVehicleById(e.getVehicleId()));
                }).collect(Collectors.toList());
    }

    /**
     * 新增停车记录
     *
     * @param sysParkingRecord 停车记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSysParkingRecord(SysParkingRecord sysParkingRecord) {
        SysParkingSpot sysParkingSpot = sysParkingSpotService.selectSysParkingSpotById(sysParkingRecord.getParkingSpotId());
        if ( sysParkingSpot.getCurrentStatus().equals(1) ) {
            throw new ServiceException("该车位已被占用");
        }
        //设置当前车位为自己占用
        sysParkingSpot.setCurrentStatus(1);
        sysParkingSpotService.updateSysParkingSpot(sysParkingSpot);
        sysParkingRecord.setCreateTime(DateUtils.getNowDate());
        return sysParkingRecordMapper.insertSysParkingRecord(sysParkingRecord);
    }

    /**
     * 修改停车记录
     *
     * @param sysParkingRecord 停车记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateSysParkingRecord(SysParkingRecord sysParkingRecord) {
        SysParkingSpot sysParkingSpot =
                sysParkingSpotService.selectSysParkingSpotById(sysParkingRecord.getParkingSpotId());
        SysParkingRecord checkRecord = this.selectSysParkingRecordById(sysParkingRecord.getId());
        if ( Objects.nonNull(checkRecord.getExitTime()) ) {
            throw new ServiceException("已出离开记录不允许修改");
        }
        if ( sysParkingSpot.getCurrentStatus().equals(1) &&
                !checkRecord.getParkingSpotId().equals(sysParkingRecord.getParkingSpotId()) ) {
            throw new ServiceException("该车位已被占用");
        }
        //更换车位
        if ( !sysParkingRecord.getParkingSpotId().equals(checkRecord.getParkingSpotId()) ) {
            sysParkingSpot.setCurrentStatus(0);
            sysParkingSpotService.updateSysParkingSpot(sysParkingSpot);
            sysParkingSpot.setId(sysParkingRecord.getParkingSpotId());
            sysParkingSpot.setCurrentStatus(1);
            sysParkingSpotService.updateSysParkingSpot(sysParkingSpot);
        }
        //离开停车场
        if ( Objects.nonNull(sysParkingRecord.getExitTime()) ) {
            sysParkingSpot.setCurrentStatus(0);
            sysParkingSpotService.updateSysParkingSpot(sysParkingSpot);
        }
        sysParkingRecord.setUpdateTime(DateUtils.getNowDate());
        return sysParkingRecordMapper.updateSysParkingRecord(sysParkingRecord);
    }

    /**
     * 批量删除停车记录
     *
     * @param ids 需要删除的停车记录主键
     * @return 结果
     */
    @Override
    public int deleteSysParkingRecordByIds(Long[] ids) {
        return sysParkingRecordMapper.deleteSysParkingRecordByIds(ids);
    }

    /**
     * 删除停车记录信息
     *
     * @param id 停车记录主键
     * @return 结果
     */
    @Override
    public int deleteSysParkingRecordById(Long id) {
        return sysParkingRecordMapper.deleteSysParkingRecordById(id);
    }
}
